package com.graduation.rescuematerial.controller;


import com.graduation.rescuematerial.service.CompanyService;
import com.graduation.rescuematerial.service.UserService;
import com.graduation.rescuematerial.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author CaiShuangLian
 * @since 2022-04-11
 */
@RestController
@RequestMapping("/company")
@Api(tags = "部门模块")
public class CompanyController {

    @Autowired
    private CompanyService companyService;
    @Autowired
    private UserService userService;


    //注意除物资系统，其余需要权限
    @ApiOperation("查询所有部门")
    @ResponseBody
    @GetMapping("/all")
    public RespBean getAll(){
        return companyService.getSysCompany();
    }

    //修改部门基本信息
    @ApiOperation("修改部门")
    @ResponseBody
    @PostMapping("/update")
    public RespBean updateCompany(@RequestBody UpdateCompanyVo updateCompanyVo, HttpServletRequest request, HttpServletResponse response){

        //获取cookie
        String ticket= updateCompanyVo.getTicket();
        if(ticket !=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null &&( userVo.getAction().equals("system_administrator") || userVo.getAction().equals("organization_administrator")) ){
                //为空字段使用原来的值(判空操作)
                if(null==updateCompanyVo.getName()||StringUtils.isBlank(updateCompanyVo.getName())){
                    updateCompanyVo.setName(userVo.getCompanyName());
                } if(null==updateCompanyVo.getTelephone()){
                    updateCompanyVo.setTelephone(userVo.getTelephone());
                }
                if(null==updateCompanyVo.getStatus()||updateCompanyVo.getStatus().booleanValue()){
                    updateCompanyVo.setStatus(userVo.getCompanyStatus());
                }
                if(null==updateCompanyVo.getAddress()||StringUtils.isBlank(updateCompanyVo.getAddress())){
                    updateCompanyVo.setAddress(userVo.getCompanyAddress());
                }
                if(null==updateCompanyVo.getDescription()||StringUtils.isBlank(updateCompanyVo.getDescription())){
                    updateCompanyVo.setDescription(userVo.getCompanyDescription());
                }
                if(null==updateCompanyVo.getLeaderPhone()){
                    updateCompanyVo.setLeaderPhone(userVo.getLeaderPhone());
                } if(null==updateCompanyVo.getLatitude()){
                    updateCompanyVo.setLatitude(userVo.getLatitude());
                }
                if(null==updateCompanyVo.getLongitude()){
                    updateCompanyVo.setLongitude(userVo.getLongitude());
                }
                if(null==updateCompanyVo.getHead()||StringUtils.isBlank(updateCompanyVo.getHead())){
                    updateCompanyVo.setHead(userVo.getCompanyHead());
                }
                return companyService.updateCompany(updateCompanyVo);
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    //获取本部门信息
    @ApiOperation("/获取本部门信息")
    @ResponseBody
    @PostMapping
    public RespBean getOwnCompany(@RequestBody String ticket,HttpServletRequest request, HttpServletResponse response){
        if(ticket !=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(null!=userVo){
                String companyName=userVo.getCompanyName();
                return companyService.getCompanyByName(companyName);
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("获取type是department的组织信息")
    @ResponseBody
    @GetMapping("/department")
    public RespBean getAllDepartment(){
        return (companyService.getAllCompanyByType("department"));
    }

    @ApiOperation("获取type是warehouse的组织信息")
    @ResponseBody
    @GetMapping("/warehouse")
    public RespBean getAllWarehouse(){
        return (companyService.getAllCompanyByType("warehouse"));
    }

    @ApiOperation("注销单位(系统管理员可以注销所有部门，注销后可再次更改状态，本部门只能注销本部门)")
    @ResponseBody
    @PostMapping("/update/status")
    public RespBean updateCompanyStatus(@RequestBody UpdateCompanyStatusVo updateCompanyStatusVo, HttpServletRequest request, HttpServletResponse response){
        String ticket=updateCompanyStatusVo.getTicket();
        if(ticket !=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(null!=userVo){
                //判断用户权限
                String action=userVo.getAction();
                //系统管理员修改权限最高
                UpdateCompanyVo updateCompanyVo=new UpdateCompanyVo();
                updateCompanyVo.setStatus(updateCompanyStatusVo.getStatus());
                updateCompanyVo.setName(updateCompanyStatusVo.getName());
                if(action.equals("system_administrator")){
                    return companyService.updateCompany(updateCompanyVo);
                }else if(action.equals("organization_administrator")){
                    if(!updateCompanyStatusVo.getName().equals(userVo.getCompanyName())){
                        return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员不可修改其他组织的状态");
                    }
                    return companyService.updateCompany(updateCompanyVo);
                }
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("联合查询部门信息")
    @ResponseBody
    @PostMapping("/query/union")
    public RespBean unionQuery(@RequestBody CompanyUnionQueryVo companyUnionQueryVo){
        //name,description模糊查询
        return companyService.unionQuery(companyUnionQueryVo);
    }

    @ApiOperation("选择部门下拉框")
    @ResponseBody
    @PostMapping("/dropDownBox")
    public RespBean companyDropDownBox(@RequestBody String ticket,HttpServletRequest request, HttpServletResponse response){
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && userVo.getAction().equals("system_administrator")){
                return companyService.companyDropDownBox();
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法添加");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }
}
